﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using FA_Server;
using FA_Model;

namespace FixedAssets.Controls
{
    public partial class MDC_Control : System.Web.UI.UserControl
    {
        FA_S_MasterDataCard s_mdc;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                s_mdc = new FA_S_MasterDataCard("JW00000001");
                this.C1Pager1.PageCount = s_mdc.lMDCS.Count;
                DataBding();
            }
        }

        private void DataBding()
        {
            int pageindex=this.C1Pager1.PageIndex;
            this.L_MasterDataCard.Text = s_mdc.lMDCS[pageindex].sMasterDataCard;
            this.L_AssetsCode.Text = s_mdc.lMDCS[pageindex].sAssetsCode;
            this.L_AssetsName.Text = s_mdc.lMDCS[pageindex].sAssetsName;
            this.L_CorpCode.Text = s_mdc.lMDCS[pageindex].sCorpCode;
            this.L_CorpName.Text = s_mdc.lMDCS[pageindex].sCorpName;
            
            this.P_Tab1.Controls.Clear();
            this.P_Tab1.Controls.Add(CreateControl_tabel(s_mdc.lMDCS[pageindex].D_Code_Name, s_mdc.lMDCS[pageindex].D_Code_Value));

            this.P_Tab2.Controls.Clear();
            //this.P_Tab2.Controls.Add(CreateControl_t(s_mdc.lMDCS[pageindex].D_Code_Name, s_mdc.lMDCS[pageindex].D_Code_Value));

            this.P_Tab3.Controls.Clear();
            //this.P_Tab3.Controls.Add(CreateControl(s_mdc.lMDCS[pageindex].D_Code_Name, s_mdc.lMDCS[pageindex].D_Code_Value, 21, 10));

            this.P_Tab4.Controls.Clear();
            //this.P_Tab4.Controls.Add(CreateControl(s_mdc.lMDCS[pageindex].D_Code_Name, s_mdc.lMDCS[pageindex].D_Code_Value, 31, 10));

        }

        protected void C1Pager1_PageIndexChanged(object sender, EventArgs e)
        {
            DataBding();
        }

        private Control CreateControl(Dictionary<string, string> dcn, Dictionary<string, string> dcv)
        {
            Control control = new Control();
            int i = 1;
            //WebControl webcontrol=new WebControl(HtmlTextWriterTag.Style);
            //webcontrol.Style=HtmlTextWriterStyle.BorderColor;
            Panel panel = new Panel();
            panel.Style.Add(HtmlTextWriterStyle.TextAlign, "Left");

            WebControl br_wc = new WebControl(HtmlTextWriterTag.Br);

            foreach (KeyValuePair<string, string> kv in dcn)
            {
                Label ln = new Label();
                ln.Text = "&nbsp;&nbsp;&nbsp;&nbsp;" + kv.Value + " :&nbsp;&nbsp;";
                ln.Style.Add(HtmlTextWriterStyle.MarginLeft, "10");
                ln.Style.Add(HtmlTextWriterStyle.BorderWidth, "2");
                Label lv = new Label();
                lv.Text = dcv[kv.Key];
                lv.Font.Underline = true;
                lv.Style.Add(HtmlTextWriterStyle.MarginLeft, "5");
                if (i % 5 == 0)
                {
                    panel.Controls.Add(ln);
                    panel.Controls.Add(lv);
                    panel.Controls.Add(br_wc);
                    control.Controls.Add(panel);
                    panel = new Panel();
                    panel.Style.Add(HtmlTextWriterStyle.TextAlign, "Left");
                }
                else
                {
                    panel.Controls.Add(ln);
                    panel.Controls.Add(lv);
                    panel.Controls.Add(br_wc);
                }
                i++;
            }
            
            return control;
        }

        private Control CreateControl_tabel(Dictionary<string, string> dcn, Dictionary<string, string> dcv)
        {
            Control control = new Control();
            int i = 1;
            //WebControl webcontrol=new WebControl(HtmlTextWriterTag.Style);
            //webcontrol.Style=HtmlTextWriterStyle.BorderColor;
            Table table = new Table();
            table.Style.Add(HtmlTextWriterStyle.TextAlign, "Left");
            table.Style.Add(HtmlTextWriterStyle.Margin, "0");
            WebControl br_wc = new WebControl(HtmlTextWriterTag.Br);
            TableRow tr = new TableRow();
            
            foreach (KeyValuePair<string, string> kv in dcn)
            {
                TableCell tc = new TableCell();
                tc.BorderWidth = 2;
                tc.BorderColor = System.Drawing.Color.CadetBlue;
                Label ln = new Label();
                ln.Text = "&nbsp;" + kv.Value + " :&nbsp;";
                ln.Style.Add(HtmlTextWriterStyle.MarginLeft, "10");
                ln.Style.Add(HtmlTextWriterStyle.BorderWidth, "2");
                Label lv = new Label();
                lv.Text = dcv[kv.Key];
                lv.Font.Underline = true;
                lv.Style.Add(HtmlTextWriterStyle.MarginLeft, "5");
                tc.Controls.Add(ln);
                tc.Controls.Add(lv);
                if (i % 5 == 0)
                {

                    tr.Controls.Add(tc);
                    table.Controls.Add(tr);
                    tr = new TableRow();
                    tr.BorderWidth = 2;
                    tr.BorderColor = System.Drawing.Color.CadetBlue;
                }
                else
                {
                    tr.Controls.Add(tc);
                }
                i++;
            }
            if (i % 5 != 0)
                table.Controls.Add(tr);

            control.Controls.Add(table);
            return control;
        }
    }
}